<template>
	<el-container>
		<!--		<el-aside width="200px" v-loading="showGrouploading">-->
		<!--			<el-container>-->
		<!--				<el-header>-->
		<!--					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>-->
		<!--				</el-header>-->
		<!--				<el-main class="nopadding">-->
		<!--					<el-tree ref="group" class="menu" node-key="id" :data="group" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>-->
		<!--				</el-main>-->
		<!--			</el-container>-->
		<!--		</el-aside>-->
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
					<!--						<el-button type="primary" plain :disabled="selection.length==0">分配角色</el-button>-->
					<!--						<el-button type="primary" plain :disabled="selection.length==0">密码重置</el-button>-->
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="params.store_name" placeholder="店铺名称" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="this.$refs.table.getData()"></el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" :params="params" @selection-change="selectionChange" stripe>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="ID" prop="id" width="80"></el-table-column>
					<el-table-column label="用户" prop="user" width="170">

						<template #default="scope">
							<el-row :gutter="20" v-if="scope.row.user">
								<el-col :span="5">
									<el-avatar :src="scope.row.user.headimgurl" size="small"></el-avatar>
								</el-col>
								<el-col :span="12">{{ scope.row.user.nickname }}</el-col>
							</el-row>
						</template>
					</el-table-column>
					<el-table-column label="权限" prop="type" width="170">

						<template #default="scope">
							{{scope.row.type==1?'商家':'个人'}}
						</template>
					</el-table-column>
					<el-table-column label="店铺图片" prop="store_logo" width="150">
						<template #default="scope">
							<el-image v-if="scope.row.type==1" style="width: 80px; height: 80px" :src="scope.row.store_logo" fit="cover">
							</el-image>
						</template>
					</el-table-column>
					<el-table-column label="店铺名称" prop="store_name" width="150"></el-table-column>
					<el-table-column label="手机号" prop="store_tel" width="150"></el-table-column>
					<el-table-column label="店铺介绍" prop="store_introduce"></el-table-column>
					<el-table-column label="所在地区" prop="store_city" width="150"></el-table-column>
					<el-table-column label="详细地址" prop="store_address"></el-table-column>

					<el-table-column label="状态" prop="status" width="150">
						<template #default="scope">
							<span v-if="scope.row.status==0">未审核</span>
							<span style="color: #67C23A" v-else-if="scope.row.status==1">审核通过</span>
							<span style="color: #F56C6C" v-else>审核不通过</span>
						</template>

					</el-table-column>

					<el-table-column label="是否关闭" prop="store_logo" width="150">
						<template #default="scope">
							<el-switch @change="isClose(scope.row)" v-model="scope.row.is_close" ></el-switch>
						</template>
					</el-table-column>

					<el-table-column label="操作" fixed="right" width="300" align="right">
						<template #default="scope">
							<template v-if="scope.row.status==0">
								<el-button type="success" size="mini" @click="shenghe(scope.row, 1)">审核通过</el-button>
								<el-button type="danger" size="mini" @click="shenghe(scope.row, 2)">审核不通过</el-button>
							</template>
							<template v-else-if="scope.row.status==1">
								<el-button type="text" size="small" @click="table_sp(scope.row, scope.$index)">视频库</el-button>
								<el-button type="text" size="small" @click="table_hd(scope.row, scope.$index)">活动列表</el-button>
								<el-button type="text" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row)">
									<template #reference>
										<el-button type="text" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</template>
							<template v-else>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row)">
									<template #reference>
										<el-button type="text" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</template>

						</template>
					</el-table-column>

				</scTable>
			</el-main>
		</el-container>
	</el-container>

	<el-dialog :title="titleMap[saveMode]" v-model="saveDialogVisible" :width="900" destroy-on-close>
		<VideoList ref="vlist" v-if="saveMode=='sp'" :mode="saveMode"></VideoList>
		<HdList ref="hdlist"  v-if="saveMode=='hd'"></HdList>
		<Save ref="save" v-if="saveMode=='edit'||saveMode=='add'" :mode="saveMode"></Save>

		<template #footer v-if="saveMode=='edit'||saveMode=='add'">
			<el-button @click="saveDialogVisible=false" >取 消</el-button>
			<el-button type="primary" @click="saveForm()" :loading="isSaveing">保 存</el-button>
		</template>
	</el-dialog>

</template>

<script>
import VideoList from './splist'
import HdList from './hdlist'
import Save from './save'
export default {
	name: 'vip_card',
	components: {
		VideoList,
		HdList,
		Save
	},
	data() {
		return {
			visible: false,
			saveDialogVisible: false,
			saveMode: 'add',
			titleMap: {
				add: "授权用户",
				hd: "活动列表",
				sp: "视频库",
				edit:"编辑"
			},

			form: {
				id:"",
				name: "",
				time_type: "1",
				tiem_days:30,
				status:'',
				desc:''
			},
			isSaveing: false,
			showGrouploading: false,
			groupFilterText: '',
			group: [],
			apiObj: [],
			selection: [],
			params: {
				store_name: ''
			},

		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
		this.getGroup()
	},
	methods: {
		isClose(v){
			var data = {id:v.id,is_close:(v.is_close?1:0)}
			this.$API.store.edit.post(data).then(res=>{
				if(res.code==200){
					this.$message.success(res.msg)

				} else {
					this.$alert(res.msg, "提示", {type: 'error'})
				}
				this.$refs.table.getData()
			})
		},
		//添加
		add(){
			this.saveMode = 'add';
			this.saveDialogVisible = true;
		},
		//编辑
		table_edit(row){
			this.saveMode = 'edit';
			this.saveDialogVisible = true;
			this.$nextTick(() => {
				//这里应该再次根据ID查询详情接口
				this.$refs.save.setData(row)
			})
		},
		table_hd(row){
			this.saveMode = 'hd';
			this.saveDialogVisible = true;
			this.$nextTick(() => {
				//这里应该再次根据ID查询详情接口
				this.$refs.hdlist.setData(row)
			})

		},
		//查看
		table_sp(row){
			this.saveMode = 'sp';
			this.saveDialogVisible = true;

			this.$nextTick(() => {
				//这里应该再次根据ID查询详情接口
				this.$refs.vlist.setData(row)
			})
		},
		//删除
		async table_del(row){
			var reqData = {id: row.id}
			var res = await this.$API.store.del.post(reqData);
			if(res.code == 200){
				this.$refs.table.getData()
				this.$message.success(res.msg)
			}else{
				this.$alert(res.msg, "提示", {type: 'error'})
			}
		},
		shenghe(row,type){
			var data = {id:row.id,status:type}
			this.$API.store.shenghe.post(data).then(res=>{
				if(res.code==200){
					this.$message.success(res.msg)
					this.$refs.table.getData()
				} else {
					this.$alert(res.msg, "提示", {type: 'error'})
				}
			});
		},
		//批量删除
		batch_del(){
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				// const loading = this.$loading();
				var ids =[]
				this.selection.forEach(item => {
					ids.push(item.id);
				})
				var reqData = {id: ids.join(',')}
				this.$API.store.del.post(reqData).then((res)=>{
					if(res.code == 200){
						this.$refs.table.getData()
						this.$message.success(res.msg)
					}else{
						this.$alert(res.msg, "提示", {type: 'error'})
					}
				});
				// loading.close();

			}).catch(() => {

			})
		},
		//提交
		saveForm(){
			this.$refs.save.submit(async (formData) => {
				console.log(formData)
                this.isSaveing = true;
				var res = await this.$API.store.edit.post(formData);
				this.isSaveing = false;
				if(res.code == 200){
					this.$refs.table.getData()
					this.saveDialogVisible = false;
					this.$message.success(res.msg)
				}else{
					this.$message.error(res.msg)
					// this.$alert(res.msg, "提示", {type: 'error'})
				}
			})
		},
		async editChange(data){
			data.visible = false
			// this.isSaveing = true;
			var res = await this.$API.user.vipcard_save.post(data);
			// this.isSaveing = false;
			if(res.code == 200){
				this.$refs.table.getData()
				// this.saveDialogVisible = false;
				this.$message.success(res.msg)
			}else{
				this.$alert(res.msg, "提示", {type: 'error'})
			}
		},
		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		//加载树数据
		async getGroup(){
			var res = await this.$API.store.list;
			this.showGrouploading = false;
			this.apiObj = res
		},
		//树过滤
		groupFilterNode(value, data){
			if (!value) return true;
			return data.label.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data){
			var params = {
				groupId: data.id
			}
			this.$refs.table.upData(params)
		},
		//搜索
		upsearch(){

		}
	}
}
</script>

<style>
</style>
